package tut.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Hacker {
	private static final Object[] EMPTY = {};

	public void reflect() throws Exception {

	 	Secret instance = new Secret("secret value");
		Class secretClass = instance.getClass();

		Method methods[] = secretClass.getDeclaredMethods();

		System.out.println("Access all the method");
		for (int i = 0; i < methods.length; i++) {
			System.out.println("Method name: " + methods[i].getName());
			System.out.println("Return type: "+ methods[i].getReturnType());
			methods[i].setAccessible(true); // ko co se loi
			System.out.println(methods[i].invoke(instance));
		}

		// print all the field name and values
		Field fields[] = secretClass.getDeclaredFields();
		System.out.println("Access all the fields");
		for (int i = 0; i < fields.length; i++) {
			System.out.println("Field name: " + fields[i].getName());
			System.out.println("Field type: " + fields[i].getType());
			fields[i].setAccessible(true); // khong co se loi
			System.out.println(fields[i].get(instance));
		}

	}
}
